/*
 * @Author: zi.yang
 * @Date: 2022-02-06 01:44:52
 * @LastEditors: zi.yang
 * @LastEditTime: 2022-02-09 10:21:19
 * @Description: 请求拦截：用户身份验证
 * @FilePath: \api-generator\app\middleware\auth.js
 */
'use strict';

module.exports = () => {
  return async function auth(ctx, next) {
    await next();
    const pathWhiteList = [ '/login', '/loginError' ];
    const notInterception = pathWhiteList.find(path => ctx.path.indexOf(path) === 0);
    if (notInterception) return;

    if (!ctx.isAuthenticated()) {
      if (ctx.request.url === '/') {
        return await ctx.redirect('/login');
      }
      await ctx.render('error/403.njk', { layout: '../layout/base.njk', msg: '身份验证失败，请重新登录' });
    }
  };
};
